package com.roy.wakeupres.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.roy.wakeupres.entity.ButterflyModel;
import com.roy.wakeupres.entity.ButterflyTask;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class Json2Object {

    private static String json = "[\n" +
            "        {\n" +
            "            \"id\": 17,\n" +
            "            \"taskName\": \"容错时间\",\n" +
            "            \"taskTypeId\": 7,\n" +
            "            \"taskDes\": \"缓冲容错时间\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 2400,\n" +
            "            \"priority\": 5,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:26:09\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 17,\n" +
            "            \"taskName\": \"交通时间\",\n" +
            "            \"taskTypeId\": 7,\n" +
            "            \"taskDes\": \"交通时间\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 2400,\n" +
            "            \"priority\": 6,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:26:09\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 1,\n" +
            "            \"taskName\": \"刷牙\",\n" +
            "            \"taskTypeId\": 1,\n" +
            "            \"taskDes\": \"刷牙\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 300,\n" +
            "            \"priority\": 3,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:24:18\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 3,\n" +
            "            \"taskName\": \"喝水\",\n" +
            "            \"taskTypeId\": 1,\n" +
            "            \"taskDes\": \"喝水\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 120,\n" +
            "            \"priority\": 3,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:25:08\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 4,\n" +
            "            \"taskName\": \"打豆浆\",\n" +
            "            \"taskTypeId\": 3,\n" +
            "            \"taskDes\": \"打豆浆\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 300,\n" +
            "            \"priority\": 3,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:25:19\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 5,\n" +
            "            \"taskName\": \"锦城湖晨跑或者闫帅奇\",\n" +
            "            \"taskTypeId\": 2,\n" +
            "            \"taskDes\": \"锦城湖晨跑或者闫帅奇\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 1800,\n" +
            "            \"priority\": 2,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:26:19\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 6,\n" +
            "            \"taskName\": \"煮鸡蛋\",\n" +
            "            \"taskTypeId\": 3,\n" +
            "            \"taskDes\": \"煮鸡蛋\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 300,\n" +
            "            \"priority\": 3,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:26:29\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 7,\n" +
            "            \"taskName\": \"计算自己的专注时间-收敛\",\n" +
            "            \"taskTypeId\": 4,\n" +
            "            \"taskDes\": \"计算自己的专注时间-收敛\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 10800,\n" +
            "            \"priority\": 1,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:25:29\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 8,\n" +
            "            \"taskName\": \"煮午饭\",\n" +
            "            \"taskTypeId\": 3,\n" +
            "            \"taskDes\": \"煮午饭\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 4200,\n" +
            "            \"priority\": 3,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:25:39\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 18,\n" +
            "            \"taskName\": \"洗碗\",\n" +
            "            \"taskTypeId\": 3,\n" +
            "            \"taskDes\": \"洗碗\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 600,\n" +
            "            \"priority\": 3,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:26:09\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 9,\n" +
            "            \"taskName\": \"午休+冥想\",\n" +
            "            \"taskTypeId\": 5,\n" +
            "            \"taskDes\": \"午休+冥想\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 1800,\n" +
            "            \"priority\": 3,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:24:38\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 11,\n" +
            "            \"taskName\": \"发散时间-学习、骑行、逛公园\",\n" +
            "            \"taskTypeId\": 5,\n" +
            "            \"taskDes\": \"发散时间-学习、骑行、逛公园\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 10800,\n" +
            "            \"priority\": 1,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:24:58\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 10,\n" +
            "            \"taskName\": \"刘耕宏\",\n" +
            "            \"taskTypeId\": 2,\n" +
            "            \"taskDes\": \"刘耕宏\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 2400,\n" +
            "            \"priority\": 2,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:24:48\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 16,\n" +
            "            \"taskName\": \"俯卧撑（薄肌）\",\n" +
            "            \"taskTypeId\": 2,\n" +
            "            \"taskDes\": \"俯卧撑薄肌\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 1200,\n" +
            "            \"priority\": 2,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:26:09\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 12,\n" +
            "            \"taskName\": \"开启一阶赌场，决战紫禁之巅\",\n" +
            "            \"taskTypeId\": 2,\n" +
            "            \"taskDes\": \"开启一阶赌场，决战紫禁之巅\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 5400,\n" +
            "            \"priority\": 3,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:24:28\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 13,\n" +
            "            \"taskName\": \"开启二阶赌场，决战紫禁之巅\",\n" +
            "            \"taskTypeId\": 6,\n" +
            "            \"taskDes\": \"开启二阶赌场，决战紫禁之巅\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 12600,\n" +
            "            \"priority\": 4,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:25:49\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 14,\n" +
            "            \"taskName\": \"洗澡\",\n" +
            "            \"taskTypeId\": 1,\n" +
            "            \"taskDes\": \"洗澡\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 1380,\n" +
            "            \"priority\": 3,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:25:59\",\n" +
            "            \"autoStart\": false\n" +
            "        },\n" +
            "        {\n" +
            "            \"id\": 15,\n" +
            "            \"taskName\": \"拉伸睡觉\",\n" +
            "            \"taskTypeId\": 2,\n" +
            "            \"taskDes\": \"拉伸睡觉\",\n" +
            "            \"taskStatus\": 0,\n" +
            "            \"total\": 600,\n" +
            "            \"priority\": 2,\n" +
            "            \"buttonTime\": null,\n" +
            "            \"createTime\": \"2024-07-17T21:04:57\",\n" +
            "            \"updateTime\": \"2024-07-19T09:26:09\",\n" +
            "            \"autoStart\": false\n" +
            "        }\n" +
            "]";

    public static List<ButterflyTask> getTask(){


        ObjectMapper mapper = new ObjectMapper();
        LocalDateTime now = LocalDateTime.now();
        List<ButterflyTask> butterflyTaskList = new ArrayList<>();
        try {
            JsonNode rootNode = mapper.readTree(json);
            if (rootNode.isArray()) {
                for (JsonNode node : rootNode) {
                    ButterflyTask butterflyTask = new ButterflyTask();
                    butterflyTask.setTaskName(node.get("taskName").asText());
                    butterflyTask.setTaskDes(node.get("taskDes").asText());
                    butterflyTask.setTaskTypeId(node.get("taskTypeId").asInt());
                    butterflyTask.setTaskStatus(node.get("taskStatus").asInt());
                    butterflyTask.setTotal(node.get("total").asInt());
                    butterflyTask.setRemaining(node.get("total").asInt());
                    butterflyTask.setPriority(node.get("priority").asInt());
                    butterflyTask.setCreateTime(now);
                    butterflyTaskList.add(butterflyTask);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return butterflyTaskList;
    }

    public static List<ButterflyTask> getTaskByDB(List<ButterflyModel> butterflyModels){

        LocalDateTime now = LocalDateTime.now();
        List<ButterflyTask> butterflyTaskList = new ArrayList<>();
        try {
            if (Objects.nonNull(butterflyModels)&&butterflyModels.size()>0) {
                for (ButterflyModel node : butterflyModels) {
                    ButterflyTask butterflyTask = new ButterflyTask();
                    butterflyTask.setTaskName(node.getTaskName());
                    butterflyTask.setTaskDes(node.getTaskDes());
                    butterflyTask.setTaskTypeId(node.getTaskTypeId());
                    butterflyTask.setTaskStatus(node.getTaskStatus());
                    butterflyTask.setTotal(node.getTotal());
                    butterflyTask.setRemaining(node.getTotal());
                    butterflyTask.setPriority(node.getPriority());
                    butterflyTask.setStar(node.getStar());
                    butterflyTask.setSettle(node.getSettle());
                    butterflyTask.setCreateTime(now);
                    butterflyTaskList.add(butterflyTask);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return butterflyTaskList;
    }

//    public static void main(String[] args) {
//
//        ObjectMapper mapper = new ObjectMapper();
//        List<ButterflyTask> butterflyTaskList = new ArrayList<>();
//        try {
//            JsonNode rootNode = mapper.readTree(json);
//            if (rootNode.isArray()) {
//                for (JsonNode node : rootNode) {
//                    ButterflyTask butterflyTask = new ButterflyTask();
//                    butterflyTask.setTaskName(node.get("taskName").asText());
//                    butterflyTask.setTaskDes(node.get("taskDes").asText());
//                    butterflyTask.setTaskTypeId(node.get("taskTypeId").asInt());
//                    butterflyTask.setTaskStatus(node.get("taskStatus").asInt());
//                    butterflyTask.setTotal(node.get("total").asInt());
//                    butterflyTask.setPriority(node.get("priority").asInt());
//                    butterflyTaskList.add(butterflyTask);
//                }
//            }
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//    }

}
